﻿using HDS.Common.Helper;
using HDS.Model;
using System.Collections.Generic;
using System.Linq;

namespace HDS.SupplierApi.Core.Entity
{
    public class SupplierRate
    {
        public SupplierRate()
        {
            this.Extras = new Dictionary<string, string>();
            this.DailyPrices = new List<DailyPrice>();
        }

        /// <summary>
        /// 供应商
        /// </summary>
        public string Supplier { get; set; }

        /// <summary>
        /// 供应商名称缩写
        /// </summary>
        public string SupplierAbbr { get; set; }

        /// <summary>
        /// 供应商酒店id
        /// </summary>
        public string HotelId { get; set; }

        /// <summary>
        /// 供应商房型id: 我方编码
        /// </summary>
        public string RoomId { get; set; }

        /// <summary>
        /// 供应商的真实房型code
        /// </summary>
        public string ThirdPartyId { get; set; }

        /// <summary>
        /// 供应商房型名称
        /// </summary>
        public string RoomName { get; set; }

        /// <summary>
        /// 标准餐食类型 MealType
        /// </summary>
        public MealType MealType { get; set; }

        /// <summary>
        /// 餐食代码
        /// </summary>
        public string MealCode { get; set; }

        /// <summary>
        /// 餐食类型说明
        /// </summary>
        public string MealDesc { get; set; }

        /// <summary>
        /// 餐食数量
        /// </summary>
        public int MealCount { get; set; }

        /// <summary>
        /// 房间可用配给
        /// </summary>
        public int Allotment { get; set; }

        /// <summary>
        /// 价格可住的最大人数
        /// </summary>
        public int MaxOccupancy { get; set; }

        /// <summary>
        /// 实价
        /// </summary>
        public Money NetPrice { get; set; }

        /// <summary>
        /// 卖价/强制价格: 酒店规定的最低销售价
        /// </summary>
        public Money SellingPrice { get; set; }

        /// <summary>
        /// 每日价格
        /// </summary>
        public List<DailyPrice> DailyPrices { get; set; }

        /// <summary>
        /// 免费取消时限，小时数
        /// <para>如果小时数为36，入住日期为10号，那么9号12点前可免费取消</para>
        /// <para>注意：各家供应商计算偏移量，酒店时区</para>
        /// </summary>
        public int FreeCancellationTimeLimit { get; set; }

        /// <summary>
        /// 预订提示信息
        /// </summary>
        public string Remarks { get; set; }

        /// <summary>
        /// 附加信息
        /// </summary>
        public Dictionary<string, string> Extras { get; set; }

        #region 价格计划

        public string RatePlanId
        {
            get
            {
                return $"{this.SupplierAbbr}:{this.HotelId}:{this.RoomId}:{(int)this.MealType}:{this.MealCount}:{this.MaxOccupancy}:{this.FreeCancellationTimeLimit}";
            }
        }

        public static SupplierRate BreakupRatePlanId(string RatePlanId, Dictionary<string, string> dicSupplierAbbr)
        {
            try
            {
                var rpArr = RatePlanId.Split(':');
                if (rpArr.Length == 7)
                {
                    if (dicSupplierAbbr.ContainsKey(rpArr[0]))
                    {
                        return new SupplierRate
                        {
                            SupplierAbbr = rpArr[0],
                            Supplier = dicSupplierAbbr[rpArr[0]],
                            HotelId = rpArr[1],
                            RoomId = rpArr[2],
                            MealType = (MealType)rpArr[3].ToInt(),
                            MealCount = rpArr[4].ToInt(),
                            MaxOccupancy = rpArr[5].ToInt(),
                            FreeCancellationTimeLimit = rpArr[6].ToInt(),
                        };
                    }
                }
            }
            catch
            {
                return null;
            }

            return null;
        }

        #endregion

        /// <summary>
        /// 移除重复项，保留低价
        /// </summary>
        /// <param name="rateList"></param>
        /// <returns></returns>
        public static List<SupplierRate> RemoveDuplicate(List<SupplierRate> rateList)
        {
            if (rateList.IsEmpty())
                return null;

            var ratePlanGroup = rateList.GroupBy(e => e.RatePlanId).Where(e => e.Count() > 1);
            var removeList = new List<SupplierRate>();
            foreach (var group in ratePlanGroup)
            {
                var _list = group.ToList();
                var maxAvails = _list.OrderByDescending(p => p.NetPrice).Take(_list.Count - 1);
                removeList.AddRange(maxAvails);
            }

            foreach (var avail in removeList)
            {
                rateList.Remove(avail);
            }

            return rateList;
        }
    }
}
